package com.xinli.xinlifm.view.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xinli.xinlifm.App;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.CategoryBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.adapter.CategoryAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class NewlessonActivity extends AppCompatActivity implements MvpView<CategoryBean> {
    public SuperModel mModel;
    public TextView category_title;
    public PullToRefreshListView pull_refresh_list;
    public CategoryAdapter mAdapter;
    public int mOffset = 0;
    public String mPage;
    public SQLiteDatabase mDatabase;
    public List<CategoryBean.DataBean> mData1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myself);
        EventBus.getDefault().register(this);
        String id = getIntent().getStringExtra("id");
        mPage=CategoryBean.class.getSimpleName()+id;
        mDatabase = App.xinLiSqlHelper.getReadableDatabase();
        Cursor cursor = mDatabase.query("cache",null,"page=?",new String[]{mPage},null,null,null);
        category_title = (TextView) findViewById(R.id.category_title);
        category_title.setText("最新心理课");
        pull_refresh_list = ((PullToRefreshListView)
                findViewById(R.id.pull_refresh_list));
        mModel = new SuperModel();
            if (cursor.moveToFirst()) {
                String content = cursor.getString(cursor.getColumnIndex("content"));
                CategoryBean categoryBean = new Gson().fromJson(content, CategoryBean.class);
                mData1 = categoryBean.getData();
                mAdapter = new CategoryAdapter(mData1, getApplicationContext());
                pull_refresh_list.setAdapter(mAdapter);
                mDatabase.close();
            }else{
                mModel.getNewLessonData(mOffset, 10, "046b6a2a43dc6ff6e770255f57328f89");
            }


        pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pull_refresh_list.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pull_refresh_list.onRefreshComplete();
                    }
                }, 1000);
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //上拉
                initData(++mOffset);
            }
        });



    }


    private void initData(int i) {
        mModel.getNewLessonData( i, 10, "046b6a2a43dc6ff6e770255f57328f89");
    }
    @Subscribe
    @Override
    public void showView(CategoryBean data) {
        mDatabase = App.xinLiSqlHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("page", mPage);
        values.put("content", new Gson().toJson(data));
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        values.put("time", time);
        mDatabase.insert("cache", null, values);
        mDatabase.close();
        final List<CategoryBean.DataBean> data1 = data.getData();
        if (mAdapter == null) {
            mAdapter = new CategoryAdapter(data1, getApplicationContext());
            pull_refresh_list.setAdapter(mAdapter);
        }
        else if (mAdapter.getCount() >= 10) {
            mAdapter.addData(data1);
            mAdapter.notifyDataSetChanged();
            pull_refresh_list.onRefreshComplete();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
